﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClientAppraise.ViewModels.Validator
{
    public class ZxValueRequestAttribute : ValidationAttribute
    {
        string errmsg;

        public ZxValueRequestAttribute(string errmsg = "此项不可以为0")
        {
            this.errmsg = errmsg;
        }

        protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
        {
            if (value == default) return ValidationResult.Success;

            var strValue = value.ToString();

            if (string.IsNullOrEmpty(strValue)) return ValidationResult.Success;

            if (!double.TryParse(strValue, out var theValue))
            {
                return new(errmsg);
            }

            if (theValue == 0)
            {
                return new(errmsg);
            }

            return ValidationResult.Success;
        }
    }
}
